package com.servlet;

import com.Mapper.UserMapper;
import com.bean.Rule;
import com.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UpdateRuleServlet")
public class UpdateRuleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        Integer freeTime = Integer.valueOf(request.getParameter("freeTime"));
        Integer costTime = Integer.valueOf(request.getParameter("costTime"));
        Integer perCost = Integer.valueOf(request.getParameter("perCost"));
        Integer dayCost = Integer.valueOf(request.getParameter("dayCost"));
        Integer monthCost = Integer.valueOf(request.getParameter("monthCost"));

        Rule rule = new Rule(freeTime, costTime, perCost, dayCost, monthCost);
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.UpdateRule(rule);
        sqlSession.commit();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }
}
